﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Power.Common;
using Power.Models;
using Power.Properties;
using Power.View;
using Power.View.Dialog;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace Power.ViewModel
{
    public class AIDistinguishViewModel : ViewModelBase
    {
        /// <summary>
        /// 图片地址
        /// </summary>
        public static string imgSrc = "/Resources/images/";

        private AIDistinguish model;
        public AIDistinguishViewModel(AIDistinguish model)
        {
            this.model = model;
            TabSwitch = new RelayCommand<object>(OnTabSwitch);
            AddWorkArea = new RelayCommand(OnAddWorkArea);
            AddVideo = new RelayCommand(OnAddVideo);
            DelWorkArea = new RelayCommand(OnDelWorkArea);
            ToReview = new RelayCommand<object>(OnToReview);
            Delete = new RelayCommand(OnDelete);
            BatchDistinguish = new RelayCommand(OnBatchDistinguish);
            SelectedAll = new RelayCommand(OnSelectedAll);
            Data = new AIDistinguishEntity
            {
                //左侧工作区
                WorkAreaList = InitWorkAreaList(new List<WorkSpaceEntity>() {
                    new WorkSpaceEntity() { Id = "1", Name = "工作区域1" },
                    new WorkSpaceEntity() { Id = "2", Name = "工作区域2" },
                    new WorkSpaceEntity() { Id = "3", Name = "工作区域3" },
                    new WorkSpaceEntity() { Id = "4", Name = "工作区域4" },
                    new WorkSpaceEntity() { Id = "5", Name = "工作区域5" },
                    new WorkSpaceEntity() { Id = "6", Name = "工作区域6" }
                }),
                //初始加载的视频列表
                VideoList = InitVideoList(new List<VideoEntity>() {
                    new VideoEntity() { Id=  "1", Progress = 50, Name = "YSJIK53-YSJIK57",Status = 1 ,Pictrue="/Resources/images/ai0.jpg"},
                    new VideoEntity() { Id=  "2", Progress = 0, Name = "YSJIK53-YSJIK58",Status = 2,  Pictrue="/Resources/images/ai0.jpg"},
                    new VideoEntity() { Id=  "3", Progress = 60, Name = "YSJIK53-YSJIK59",Status = 3 ,Pictrue="/Resources/images/ai0.jpg"},
                    new VideoEntity() { Id=  "4", Progress = 70, Name = "YSJIK53-YSJIK59",Status = 3 ,Pictrue="/Resources/images/ai0.jpg"},
                    new VideoEntity() { Id=  "5", Progress = 80, Name = "YSJIK53-YSJIK59",Status = 3 ,Pictrue="/Resources/images/ai0.jpg"},
                    new VideoEntity() { Id=  "6", Progress = 90, Name = "YSJIK53-YSJIK59",Status = 3 ,Pictrue="/Resources/images/ai0.jpg"},
                }),
                //区域下拉框1
                RegionSelectList = new ObservableCollection<SelectListItem>() { new SelectListItem { SelectItem = "所有区域", SelectID = "0" },
                    new SelectListItem { SelectItem = "所有区域1", SelectID = "1" }, new SelectListItem { SelectItem = "所有区域2", SelectID = "2" },
                    new SelectListItem { SelectItem = "所有区域3", SelectID = "3" } },
                //区块下拉框2
                BlockSelectList = new List<SelectListItem>() { new SelectListItem { SelectItem = "区块", SelectID = "0" },
                    new SelectListItem { SelectItem = "区块1", SelectID = "1" }, new SelectListItem { SelectItem = "区块2", SelectID = "2" },
                    new SelectListItem { SelectItem = "区块3", SelectID = "3" } },
                //工点下拉框3
                WorkSelectList = new List<SelectListItem>() { new SelectListItem { SelectItem = "工点", SelectID = "0" },
                    new SelectListItem { SelectItem = "工点1", SelectID = "1" }, new SelectListItem { SelectItem = "工点2", SelectID = "2" },
                    new SelectListItem { SelectItem = "工点3", SelectID = "3" } },
                //视频下拉框4
                VideoSelectList = new List<SelectListItem>() { new SelectListItem { SelectItem = "视频", SelectID = "0" },
                    new SelectListItem { SelectItem = "视频1", SelectID = "1" }, new SelectListItem { SelectItem = "视频2", SelectID = "2" },
                    new SelectListItem { SelectItem = "视频3", SelectID = "3" } }
            };
        }

        //点击图片跳转时传入ID
        private void OnToReview(object sender)
        {
            //获取当前选中的工作区
            var item = this.Data.WorkAreaList.Where(a => a.BorderBackground == "#0087D3").FirstOrDefault();
            if (item != null) { 
                string ids = item.Id + "|";
                if (sender != null) {
                    ids += Convert.ToString(((Button)sender).Tag);
                }
                var model = new AIDistinguishDetail(ids);
                ((Window.GetWindow(this.model) as MainWindow).DataContext as MainViewModel).UserContent = model;
            }
        }

        /// <summary>
        /// 添加工作区
        /// </summary>
        private void OnAddWorkArea()
        {
            AddWorkArea box = new AddWorkArea();
            var result = box.ShowDialog();
        }
        /// <summary>
        /// 添加条件视频
        /// </summary>
        private void OnAddVideo()
        {
            AddVideo box = new AddVideo();
            var result = box.ShowDialog();
        }
        /// <summary>
        /// 删除
        /// </summary>
        private void OnDelete()
        {
            var selectedList = Data.VideoList.Where(p => p.IsChecked == true).ToList();
            if (selectedList == null || selectedList.Count == 0)
            {
                MessagePrompt box = new MessagePrompt("请选择要删除的行");
                box.ShowDialog();
                return;
            }
            { 
                MessagePrompt box = new MessagePrompt("是否确认删除？", true);
                var result = box.ShowDialog();
                if (result ==true)
                {
                    for (int i = 0; i < selectedList.Count; i++)
                    {
                        var item = selectedList[i] ;
                        Data.VideoList.Remove(item);
                    }
                }
            }
        }
        /// <summary>
        /// 批量识别
        /// </summary>
        private void OnBatchDistinguish()
        {
            var selectedList = Data.VideoList.Where(p => p.IsChecked == true).ToList();
            if (selectedList == null || selectedList.Count == 0)
            {
                MessagePrompt box1 = new MessagePrompt("请选择要识别的行");
                box1.ShowDialog();
                return;
            }
            MessagePrompt box = new MessagePrompt("操作成功");
            box.ShowDialog();
        }
        /// <summary>
        /// 全选
        /// </summary>
        private void OnSelectedAll()
        {
            if (Data.VideoList.Where(p => p.IsChecked == true).Count() == Data.VideoList.Count)
            {
                foreach (var item in Data.VideoList)
                {
                    item.IsChecked = false;
                }

            }
            else
            {
                foreach (var item in Data.VideoList)
                {
                    item.IsChecked = true;
                }
            }
           
            
        }

        /// <summary>
        /// 删除工作区
        /// </summary>
        private void OnDelWorkArea()
        {
            var ownerWindow = Window.GetWindow(model);
            var item = this.Data.WorkAreaList.Where(a=>a.BorderBackground == "#0087D3").FirstOrDefault();

            MessagePrompt box = new MessagePrompt("是否删除该工作区域？", true);
            box.ShowDialog();
        }

        /// <summary>
        /// 页面切换
        /// </summary>
        /// <param name="sender"></param>
        private void OnTabSwitch(object sender)
        {
            string imgSrc = "/Resources/images/";
            Button curButton = (Button)sender;
            foreach (var item in Data.WorkAreaList)
            {
                if (item.Id == curButton.Tag.ToString())
                {
                    //设置选中
                    item.ImageSrcConLeft = $"{imgSrc}dcl11.png";
                    item.ImageSrcConRight = $"{imgSrc}dcl99.png";
                    item.BorderBackground = "#0087D3";
                    item.LabelForeground = "#FFFFFF";
                    ///请根据业务处理视频列表逻辑
                    //Data.VideoList = InitVideoList();
                }
                else { 
                    item.ImageSrcConLeft = $"{imgSrc}dcl1.png";
                    item.ImageSrcConRight = $"{imgSrc}dcl9.png";
                    item.BorderBackground = "Transparent";
                    item.LabelForeground = "#7C8288";
                }
            }
        }

        #region Command
        public RelayCommand<object> TabSwitch { get; private set; }
        public RelayCommand AddWorkArea { get; private set; }
        public RelayCommand SelectedAll { get; private set; }
        public RelayCommand AddVideo { get; private set; }
        public RelayCommand DelWorkArea { get; private set; }

        public RelayCommand<object> ToReview { get; private set; }
        public RelayCommand Delete { get; private set; }
        public RelayCommand BatchDistinguish { get; private set; }
        #endregion

        #region 数据绑定
        public AIDistinguishEntity Data { get; private set;  }

        #region 数据绑定辅助方法
        /// <summary>
        /// 工作区域
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public ObservableCollection<WorkSpaceEntity> InitWorkAreaList(List<WorkSpaceEntity> list) {
            ObservableCollection<WorkSpaceEntity> result = new ObservableCollection<WorkSpaceEntity>();
            if (list == null)
                return result;
            for (int i = 0; i < list.Count; i++)
            {
                //默认选中第0个
                if (i == 0)
                {
                    list[i].CornerRadius = "15 15 0 0";
                    list[i].ImageSrcConLeft = $"{imgSrc}dcl11.png";
                    list[i].ImageSrcConRight = $"{imgSrc}dcl99.png";
                    list[i].BorderBackground = "#0087D3";
                    list[i].LabelForeground = "#FFFFFF";
                }
                else {
                    list[i].CornerRadius = "0";
                    list[i].ImageSrcConLeft = $"{imgSrc}dcl1.png";
                    list[i].ImageSrcConRight = $"{imgSrc}dcl9.png";
                    list[i].BorderBackground = "Transparent";
                    list[i].LabelForeground = "#7C8288";
                }
                result.Add(list[i]);
            }
            return result;
        }

        /// <summary>
        /// 视频列表
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public ObservableCollection<VideoEntity> InitVideoList(List<VideoEntity> list)
        {
            ObservableCollection<VideoEntity> result = new ObservableCollection<VideoEntity>();
            if (list == null)
                return result;
            foreach (var item in list) {
                item.ProgressDesc = $"{item.Progress}%";
                item.BorderWidth = $"{item.Progress}*" ;
                item.BorderWidth2 = $"{100-item.Progress}*";
                switch (item.Status)
                {
                    case 1:
                        item.PathFill = "#0087d2";
                        item.StatusDesc = "AI识别";
                        item.ProgressType = "识别进度";
                        break;
                    case 2:
                        item.PathFill = "#ff9100";
                        item.StatusDesc = "正在检测";
                        item.ProgressType = "检测进度";
                        break;
                    case 3:
                        item.PathFill = "#ff5555";
                        item.StatusDesc = "待复核";
                        item.ProgressType = "复核进度";
                        break;
                    default:
                        item.PathFill = "Transparent";
                        item.StatusDesc = "";
                        break;
                }
                result.Add(item);
            }
            return result;
        }
        #endregion
        #endregion
    }
}
